package com.shop.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
//创建数据库封装类，定义连接、关闭等方法
public class DBConnection {
	private static final String DBDRIVER = "com.mysql.jdbc.Driver";	//驱动串
	private static final String DBURL = "jdbc:mysql://localhost:3306/shopmarket";	//连接URL
	private static final String DBUSER = "root";
	private static final String DBPASSWORD = "123456";
	//定义连接数据库方法
	public static Connection getConnection(){
		Connection conn = null;	//声明一个连接对象
		try{
			Class.forName(DBDRIVER);	//注册驱动
		conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
		}catch(ClassNotFoundException e){
			e.printStackTrace();
		}catch(SQLException e){
			e.printStackTrace();
		}
		return conn;
	}
	//定义关闭连接方法
	public static void close(Connection conn){
		if(conn!=null){
			try{
				conn.close();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
	}
	//定义关闭预处理方法
	public static void close(PreparedStatement pstmt){
		if(pstmt!=null){
			try{
				pstmt.close();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
	}
	//定义关闭结果集方法
	public static void close(ResultSet rs){
		if(rs!=null){
			try{
				rs.close();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
	}
}
